var app = getApp();
Page({
  data: {
    dataItem: '抄读数据信息',
    numList: [],
    readoutlist: [],
    terminalNo: '',
    varType: '',
    radioVal: '',
    moreSjflag: true,
    moreChflag: true,
    userInfo: {}
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    var that = this;
    that.setData({ userInfo: app.userInfo });
    that.checkLogin();
  },
  checkLogin: function () {
    var name = wx.getStorageSync('userName')
    if (name == '' || name == null) {
      wx.showModal({
        title: '提示',
        content: "用户未登录",
        confirmText: '去登录',
        showCancel: false,
        success: function () {
          wx.navigateTo({
            url: '../login/login',
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  showMoreSj: function () {
    this.setData({ moreSjflag: false })
  },
  hideMoreSj: function () {
    this.setData({ moreSjflag: true })
  },
  showMoreCh: function () { //获取终端编号列表
    var that = this;
    var param = { userName: wx.getStorageSync('userName')}
    var header = { 'content-type': 'application/x-www-form-urlencoded' };
    wx.request({
      url: app.url + '/xywl/appUserMeters.htm',
      data: app.ecbEncrypto(param),
      method: 'POST',
      header: header,
      success: function(res){
        var decodingStr = app.ecbDecrypto(res.data)
        that.setData({ moreChflag: false })
        var rest = JSON.parse(decodingStr)
        if(rest.res==0){
          var list = rest.gasMeterList
          var nlist = []
          for(var i = 0; i < list.length; i++){
            nlist.push(list[i][0])
          }
          that.setData({ numList: nlist })
        }else{
          that.setData({ numList: []})
        }
      }
    })
  },
  hideMoreCh: function () {
    this.setData({ moreChflag: true })
  },
  getTerminalNo: function(e){
    this.setData({terminalNo:e.detail.value});
  },
  //选择数据项
  getValue:function(e){
    var param = e.currentTarget.dataset.vartype;
    var value = '抄读实时状态信息';
    if(param == 3){
      value = '抄读实时状态信息';
    }else if(param == 0){
      value = '抄读总路基本信息';
    }else if(param == 5){
      value = '抄读支路电流信息';
    }
    this.setData({dataItem:value,varType:param});
  },
  clickFun: function (e){
    var name = e.currentTarget.dataset.name;
    this.setData({radioVal:name});
  },
  sureButton: function(){
    var that = this;
    that.setData({ terminalNo: that.data.radioVal, moreChflag: true});
  },
  //抄读查询
  readout: function(){
    var that = this;
    var meterAddr = that.data.terminalNo;
    var varType = that.data.varType;
    if (varType==''){
      wx.showToast({
        title: '选择数据项',
        image: '../../images/error.png',
        duration: 2000,
        mask: true
      })
      return false;
    }
    if (meterAddr==''){
      wx.showToast({
        title: '选择终端编号',
        image: '../../images/error.png',
        duration: 2000,
        mask: true
      })
      return false;
    }
    var param = { "meterAddr": meterAddr, "varType": varType, "dataType": "0" }
    var header = { 'content-type': 'application/x-www-form-urlencoded' };
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    wx.request({
      url: app.url + '/xywl/realTimeReading.htm',
      data: app.ecbEncrypto(param),
      method: 'POST',
      header: header,
      success: function (res) {
        wx.hideLoading();
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        if (rest.res == 0) {
          var resp = rest.respone;
          if(resp.strBackFlag==1){
            that.setData({ readoutlist: rest.respone.dataList });
          }else{
            //暂无数据
            that.setData({ readoutlist: [] });
          }
        }else{
          //暂无数据
          that.setData({ readoutlist: [] });
        }
      }
    })
  }
});